#!/usr/bin/env python
#
# Copyright 2010 Steve Leach
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#

import sys
import time
import datetime
import csv

from formatting import format_thousands
from eveapi import EVEAPIConnection

def main():
	print('API Info Account Checker')
	if len(sys.argv) < 3:
		show_help()
		exit(-1)
	account = sys.argv[1]
	api_key = sys.argv[2] 

	print
	print 'Account:  ' + account
	print 'API Key:  ' + api_key
	print 'Time now: ' + time.strftime('%Y-%m-%d %H:%M:%S')

	api = EVEAPIConnection()

	skilltree = api.eve.SkillTree()

	skilltree.skill_reqs = {}
	try:
		reqsReader = csv.reader(open('skillreqs.txt', 'rb'))
		for row in reqsReader:
			skilltree.skill_reqs[row[0]] = int(row[1])
	except:
		print 'Unable to load skill requirements from skillreqs.txt'

	auth = api.auth(userID=account, apiKey=api_key)

	characters = auth.account.Characters().characters 

	for char in characters:
		show_character(auth, char.characterID, skilltree)
	
	print
	print( '=================' )
	print 'Time now: ' + time.strftime('%Y-%m-%d %H:%M:%S')
	print 'End.'
	print

def show_character(auth, char_id, skilltree):
	char = auth.character(char_id)

	show_char_sheet(char, skilltree)
	show_wallet(char)
	show_kills(char)
	show_contacts(char)

def format_date(date_val):
	return time.strftime('%Y-%m-%d', time.gmtime(date_val) )

def show_char_sheet(char, skilltree):
	char_sheet = char.CharacterSheet()
	print
	print( '=================' )
	print( 'Character Name: ' + char_sheet.name )
	print
	print( 'Character ID:   ' + str(char_sheet.characterID) )
	print( 'Current corp:   ' + char_sheet.corporationName )
	print( 'Date of Birth:  ' + format_date(char_sheet.DoB) )
	print( 'Balance:        ' + format_thousands(char_sheet.balance)+' isk' )
	print
	print( 'Intelligence:   ' + str(char_sheet.attributes.intelligence) )
	print( 'Willpower:      ' + str(char_sheet.attributes.willpower) )
	print( 'Memory:         ' + str(char_sheet.attributes.memory) )
	print( 'Perception:     ' + str(char_sheet.attributes.perception) )
	print( 'Charisma:       ' + str(char_sheet.attributes.charisma) )
	print

	print( 'Skills summary...' )
	for g in skilltree.skillGroups:

		# ... iterate over the skills in this group...
		for skill in g.skills:

			if skill.typeName in skilltree.skill_reqs:
				# see if we trained this skill by checking the character sheet object
				req_level = skilltree.skill_reqs[skill.typeName]
				trained = char_sheet.skills.Get(skill.typeID, False)
				if trained:
					if req_level == 0:
						print '  ' + skill.typeName + ' (' + str(trained.level) + ')'
					elif trained.level < req_level:
						print '  Too low: ' + skill.typeName + ' (' + str(trained.level) + ')'
				else:
					if req_level > 0:
						print '  Missing: ' + skill.typeName

	print

def show_wallet(char):
	journal = char.WalletJournal()

	entriesByRefType = journal.transactions.GroupedBy("refTypeID")

	print( 'Player donations...' )
	try:
		playerDonationID = 10
		for amount, date, name1, name2 in entriesByRefType[playerDonationID].Select("amount", "date", "ownerName1", "ownerName2"):
			print '  ' + format_thousands(int(amount)) + ' isk, ' + format_date(date) + ', ' + name1 + ' to ' + name2
	except KeyError:
		print '  *** No recent player donations found ***'

	print

def show_kills(char):
	print 'Recent kills...'

	try:
		kill_log = char.KillLog()

		for kill in kill_log.kills:
			print format_date(kill.killTime), victim
	except:
		print '  *** Unable to list kills ***'

	print

def show_contacts(char):
	print 'High standing contacts...'

	try:
		contacts = char.ContactList()

		for contact in contacts.contactList:
			if contact.standing > 0:
				print '  ' + contact.contactName + ' (' + str(contact.standing) + ')'
	except Exception, e:
		print '  *** Unable to list contacts *** ' + e.message

	print

def show_help():
	print( 'Usage: api_checker {account} {api_key}' )


if __name__ == '__main__':
	main()

#
# End
#
